package com.zksk.admin.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zksk.admin.domain.system.vo.AreaTreeVo;
import com.zksk.admin.domain.system.vo.AreaVo;
import com.zksk.admin.service.system.ISysDictAreaService;
import com.zksk.common.core.constant.CacheConstants;
import com.zksk.common.core.constant.ProductConstants;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.core.web.domain.AjaxResult;
import com.zksk.common.redis.service.RedisService;
import com.zksk.mbg.admin.domain.ProductEnum;
import com.zksk.mbg.admin.domain.SysDictArea;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 中国行政地区 接口
 * </p>
 *
 * @author Zksk Acmen
 * @since 2022-12-08
 */
@RequestMapping("/area")
@Api(tags = "地址相关接口")
@RestController
public class SysDictAreaController extends BaseController {

    @Autowired
    private ISysDictAreaService dictAreaService;

    @Autowired
    private RedisService redisService;

    /**
     * 一次性返回省市 tree
     */
    @GetMapping
    @ApiOperation(value = "一次性返回省市数据")
    public AjaxResult tree() {
        List<AreaVo> areaVos = redisService.getCacheList(CacheConstants.REDIS_AREA_TREE_KEY + "all");
        if (areaVos.isEmpty()) {
            // 获取前二级的所有数据
            // level 0 省 1 市
            QueryWrapper<SysDictArea> wrapper = new QueryWrapper<SysDictArea>()
                    .in(SysDictArea.LEVEL, Arrays.asList(0, 1))
                    .orderByAsc(SysDictArea.ID);
            List<SysDictArea> list = dictAreaService.list(wrapper);
            AreaTreeVo areaTreeVo = new AreaTreeVo();
            areaVos = areaTreeVo.areaList(list);
            //将Students转换为json格式
            redisService.setCacheList(CacheConstants.REDIS_AREA_TREE_KEY + "all", areaVos);
        }
        return success(areaVos);
    }

    /**
     * 获取全国省/市/区
     */
    @GetMapping("/lv{level}/list")
    @ApiOperation(value = "获取列表 0 省份 1 市 2 区县")
    public AjaxResult list(@PathVariable("level") Integer level) {
        List<SysDictArea> list = redisService.getCacheList(CacheConstants.REDIS_AREA_TREE_KEY + level);
        if (list.isEmpty()) {
            // level 0 省 1 市 2 区
            QueryWrapper<SysDictArea> wrapper = new QueryWrapper<SysDictArea>()
                    .eq(SysDictArea.LEVEL, level)
                    .orderByAsc(SysDictArea.ID);
            list = dictAreaService.list(wrapper);
            redisService.setCacheList(CacheConstants.REDIS_AREA_TREE_KEY + level, list);
        }

        return success(list);
    }

    /**
     * 获取下一级全部
     */
    @GetMapping("/{pcode}/child")
    @ApiOperation(value = "获取下一层行政级别的所有记录")
    public AjaxResult childList(@PathVariable("pcode") String pcode) {
        List<SysDictArea> list = redisService.getCacheList(CacheConstants.REDIS_AREA_TREE_KEY + pcode);
        if (list.isEmpty()) {
            // level 0 省 1 市 2 区
            QueryWrapper<SysDictArea> wrapper = new QueryWrapper<SysDictArea>()
                    .eq(SysDictArea.PARENT_CODE, pcode)
                    .orderByAsc(SysDictArea.ID);
            list = dictAreaService.list(wrapper);
            redisService.setCacheList(CacheConstants.REDIS_AREA_TREE_KEY + pcode, list);
        }
        return success(list);
    }

}
